<!DOCTYPE html>
<html>

<head lang="en">
    <title>攻略文章</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/strategyDetails.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <script src="/js/common.js"></script>


    <script>
        $(function () {
            var type;
            var pages = 2;//总页数
            var Arr = [];//用户存储游记文章和攻略文章的容器
            var collect = false; //标记是已收藏还是没收藏，告知后台去删或新增


            var params = getParams();
            //根据攻略文章id查询攻略文章
            $.get(baseUrl + '/strategyDetails/' + params.id, function (data) {
                $(".detail").renderValues(data);
            })
            /*getEcho: function (item,value) {
                if(value){
                    $("#sctxt").text('取消收藏');
                    collect = true;
                }else {
                    $("#sctxt").text('收藏');
                    collect = false;
                }*/

            //进页面就发ajax去后台查，如果已经收藏，就显示取消收藏，如果没，就显示收藏
            $.get(baseUrl + '/collects/code', {strategydetailId: params.id}, function (data) {
                console.log(data);
                if (data.code) {
                    $("#sctxt").text('取消收藏');
                    collect = true;
                } else {
                    $("#sctxt").text('收藏');
                    collect = false;
                }
            })

            //给收藏按钮绑定事件，被点击就去后台删除或新增，并将按钮的字面改变
            $("#sctxt").click(function () {
                //判断sessionStorage对象中是否有user对象
                var userStr = sessionStorage.getItem("user");
                var user;
                if (userStr) {//如果有登录,就转为json对象
                    user = JSON.parse(userStr);
                } else {
                    window.location.href = "/login.html";
                }
                /*//获取循环中绑定在收藏按钮上的文章id
                strategydetailId = $("#mir").data('id');*/

                console.log("文章id    "+params.id);//打印一下文章id
                $.post(baseUrl + '/collects', {
                    collect: collect,
                    strategydetailId: params.id
                }, function (data) {
                    if (collect) {
                        $("#sctxt").text('收藏');
                        collect = false;
                    } else {
                        $("#sctxt").text('取消收藏');
                        collect = true;
                    }
                })
            });


        });
    </script>
</head>

<body>

<div class=" nav-search">
    <a href="javascript:history.go(-1);">
        <span><i class="fa fa-chevron-left"></i></span>
    </a>
</div>
<hr>

<div class="container detail">
    <h4 class="title"><strong render-html="title"></strong></h4>

    <img class="head-img" render-src="coverUrl">
    <div class="author">By <span></span></div>
    <div class="content" render-html="strategyContent.content">
    </div>
</div>
<div>
    <a id="mir" class="btn btn-danger"
       render-fun="getCollect" render-key="list.id"
       style="color:#fff;">
        <span id="sctxt" render-fun="getEcho" render-key="list.isCollect">收藏</span>
        <i class="iconfont icon-favorite"></i>
    </a>
</div>


</body>

</html>